﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using Drawer.Common;

namespace Drawer.Controls
{
    /// <summary>
    /// Interaction logic for Toolbar.xaml
    /// </summary>
    public partial class ToolBar : UserControl
    {
        #region Events

        public event Action<BaseShape> SelectedShapeChanged;
        public event Action<Brush> SelectedOutlineColorChanged;
        public event Action<Brush> SelectedFillColorChanged;

        #endregion Events

        #region Variables

        private List<string> availableShapes;
        private List<ToggleButton> availableShapesButtons;
        private ColorPicker outlineColorPicker;
        private ColorPicker fillColorPicker;

        #endregion Variables

        #region Properties

        private Brush selectedOutlineColor;
        public Brush SelectedOutlineColor
        {
            get
            {
                return selectedOutlineColor;
            }
            set
            {
                // Set value
                selectedOutlineColor = value;                
                outlineColorPicker.SelectedColor = value;
                // Announce event
                if (SelectedOutlineColorChanged != null)
                    SelectedOutlineColorChanged(value);
            }
        }

        private Brush selectedFillColor;
        public Brush SelectedFillColor
        {
            get
            {
                return fillColorPicker.SelectedColor;
            }
            set
            {
                // Set value
                selectedFillColor = value;
                fillColorPicker.SelectedColor = value;
                // Announce event
                if (SelectedOutlineColorChanged != null)
                    SelectedFillColorChanged(value);
            }
        }

        private BaseShape selectedShape;
        public BaseShape SelectedShape
        {
            get
            {
                return selectedShape;
            }
            set
            {
                // Set value
                selectedShape = value;
                // Annouce event
                if (SelectedShapeChanged != null)
                    SelectedShapeChanged(value);
            }
        }

        #endregion Properties

        public ToolBar()
        {
            InitializeComponent();

            BuildToolbar();
        }

        private void BuildToolbar()
        {
            // Get all Shape classes
            availableShapes = Utils.GetAllShapeClasses();

            // Clear ToolBar items
            toolBar.Items.Clear();

            // Create ShapeChooser Panel
            ListBox listBox = new ListBox
            {
                Height = 54,
                Width = 130,
                ItemsPanel = (ItemsPanelTemplate)Resources["ListBoxItemsPanelTemplate"],
                ItemContainerStyle = (Style)Resources["ListBoxItemContainerStyle"]
            };
            // Create ToggleButton to choose Shape to draw
            BaseShape shape;
            ToggleButton button;
            availableShapesButtons = new List<ToggleButton>();
            foreach (var item in availableShapes)
            {
                // Create sample Shape to draw into Button
                shape = (BaseShape)Utils.CreateShapeFromClassName(item);
                shape.StartPoint = new Point(0, 0);
                shape.Dimension = new Dimension(20, 20);
                shape.OutlineColor = Brushes.SteelBlue;
                shape.FillColor = (Brush)Resources["ButtonShapeFill"];
                shape.StrokeSize = 1;

                // Create ToggleButton
                button = new ToggleButton();
                button.Style = (Style)Resources["ToggleButtonStyle"];
                button.Click += ShapeButton_Click;
                button.Content = shape.GetShape();
                button.Uid = item;

                // Add Button to ListBox
                listBox.Items.Add(button);
                // Add Button to List for future use
                availableShapesButtons.Add(button);
            }
            // Add ShapeChooser Panel to ToolBar
            toolBar.Items.Add(listBox);

            // Create ColorPicker Panel
            StackPanel colorPanel = new StackPanel
            {
                Orientation = Orientation.Vertical,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch
            };
            // Create ColorPicker: one for Outline Color, one for Fill Color
            outlineColorPicker = new ColorPicker(Brushes.Transparent, "Shape Outline");
            fillColorPicker = new ColorPicker(Brushes.Transparent, "Shape Fill");
            // Add SelectedColorChanged events to ColorPicker
            outlineColorPicker.SelectedColorChanged += OutlineColorPicker_SelectedColorChanged;
            fillColorPicker.SelectedColorChanged += FillColorPicker_SelectedColorChanged;
            // Add ColorPicker to Panel
            colorPanel.Children.Add(outlineColorPicker);
            colorPanel.Children.Add(fillColorPicker);
            // Add ColorPicker Panel to ToolBar
            toolBar.Items.Add(colorPanel);

            //toolBar.Items.Add(new StrokePicker());
        }

        #region Events handler

        private void ShapeButton_Click(object sender, RoutedEventArgs e)
        {
            ToggleButton sourceButton = sender as ToggleButton;
            if (sourceButton.IsChecked == true)
            {
                // Set all ToggleButton IsChecked to false
                foreach (var item in availableShapesButtons)
                {
                    item.IsChecked = false;
                }
                // Set senderButton IsChecked to true
                sourceButton.IsChecked = true;
                // Set SelectedShape property
                SelectedShape = (BaseShape)Utils.CreateShapeFromClassName(sourceButton.Uid);
            }
            else
            {
                // Set SelectedShape property
                SelectedShape = null;
            }
        }

        private void OutlineColorPicker_SelectedColorChanged(Brush obj)
        {
            if (obj != selectedOutlineColor)
                SelectedOutlineColor = obj;
        }

        private void FillColorPicker_SelectedColorChanged(Brush obj)
        {
            if (obj != selectedFillColor)
                SelectedFillColor = obj;
        }

        #endregion Events handler
    }
}
